package com.hluck.criminalintent

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.hluck.criminalintent.model.Crime
import androidx.databinding.DataBindingUtil
import com.hluck.criminalintent.databinding.FragmentCrimeBinding


class CrimeFragment : Fragment() {
    private val TAG = "CrimeFragment"
    private lateinit var crime:Crime
    private var binding:FragmentCrimeBinding? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        crime = Crime()
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_crime, container, false)
        binding = DataBindingUtil.bind(view)
        binding?.btnCrimeDate?.apply {
            text = crime.date.toString()
            isEnabled = false
        }
        binding?.checkboxCrimeSolved?.setOnCheckedChangeListener { _, isSolved ->
            crime.isSolved = isSolved
        }
        return binding?.root
    }

    override fun onStart() {
        super.onStart()
        val textWatcher = object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                crime.title = p0.toString()
                Log.d(TAG, "onTextChanged: ${crime.title}")
            }

            override fun afterTextChanged(p0: Editable?) {}
        }
        binding?.editCrimeTitle?.addTextChangedListener(textWatcher)
    }

}